var LEAVFILE="data/leave.xml";
var DEFLEAVFILE="defs/leave.xml";
var LEAVDOM=new ActiveXObject("Msxml2.DOMDocument");;
var LEAVROOT="";

var LMONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dec"];
var ALLMONTHS=["All","Jan","Feb","Mar","Apr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dec"];

if(!LEAVDOM) {
  alert("Error creating object XMLLEAVE");
}

function LoadLeave() {
  if(dos_loadxmlinit(LEAVDOM,DEFLEAVFILE,LEAVFILE)=="error") {
    return 0;
  }
  LEAVROOT=LEAVDOM.selectSingleNode("leave");
  if(!LEAVROOT.selectSingleNode("year[ @num=\""+GetPrefYear()+"\"]")) {
    CreateLeaveYear(GetPrefYear());
  }
  if(!LEAVROOT) {
    alert("No root");
  }
}


function CreateLeaveYear(year) {
    var ny=LEAVDOM.createElement("year");
    LEAVROOT.appendChild(ny);
    ny.setAttribute("num",year);
    for(var m=1; m <=12 ; m++) {
      var nm=LEAVDOM.createElement("month");
      ny.appendChild(nm);
      nm.setAttribute("num",m);
    }     
    LEAVDOM.save(LEAVFILE);
}
 

function GetPrefYear() {
  return LEAVROOT.getAttribute("prefyear") || (new Date()).getFullYear();
}

function SetPrefYear(year) {
  LEAVROOT.setAttribute("prefyear",year);
  if(!LEAVROOT.selectSingleNode("year[ @num=\""+GetPrefYear()+"\"]")) {
    CreateLeaveYear(GetPrefYear());
  }
  LEAVDOM.save(LEAVFILE);
}

function GetPrefMonth() {
  return LEAVROOT.getAttribute("prefmonth") || (1+(new Date()).getMonth());
}

function SetPrefMonth(m) {
  LEAVROOT.setAttribute("prefmonth",m);
  LEAVDOM.save(LEAVFILE);
}



function SetLeaveField(year,month,field,val) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]/month[@num =\""+month+"\"]");
  if(leave) {
    leave.setAttribute(field,val);
    LEAVDOM.save(LEAVFILE);
  }
}

function GetLeaveField(year,month,field,val) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]/month[@num =\""+month+"\"]");
  if(leave) {
    return leave.getAttribute(field);
  }
  return 0;
}

function GetPrevLeave(year) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]");
  if(leave) {
    return leave.getAttribute("prev") || 0;
  }
  return 0;
}

function SetPrevLeave(year,prev) {
  LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]").setAttribute("prev",prev);
  LEAVDOM.save(LEAVFILE);
}

function GetInitLeave(year) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]");
  if(leave) {
    return leave.getAttribute("init") || 0;
  }
  return 0;
}

function SetInitLeave(year,prev) {
  LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]").setAttribute("init",prev);
  LEAVDOM.save(LEAVFILE);
}


function GetYearTgt(year,field) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]");
  if(leave) {
    return leave.getAttribute(field) || 0;
  }
  return 0;
}

function SetYearTgt(year,field,val) {
  LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]").setAttribute(field,val);
  LEAVDOM.save(LEAVFILE);
}

function SetYearDone(year,field,val) {
  LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]").setAttribute(field,val);
  LEAVDOM.save(LEAVFILE);
}

function GetYearDone(year,field) {
  var leave=LEAVROOT.selectSingleNode("year[ @num=\""+year+"\"]");
  if(leave) {
    return leave.getAttribute(field) || 0;
  }
  return 0;
}


function leave_iterator(year) {
  this._list=LEAVROOT.selectSingleNode("year[@num=\""+year+"\"]").childNodes; 
  this._idx=0;
  this._end=12;
}

leave_iterator.prototype.end = function() {return this._idx>= this._end;}
leave_iterator.prototype.next = function() { this._idx++ ;}

leave_iterator.prototype.seek          = function(m) { if(m<=12 && m>=1) this._idx=m-1;}
leave_iterator.prototype.getMonthName  = function () { return LMONTHS[this._idx];}
leave_iterator.prototype.getMonth   = function() { return this._idx+1; }

leave_iterator.prototype.getField = function(fname) {return this._list[this._idx].getAttribute(fname) || "0";}
leave_iterator.prototype.setField = function(fname,val) { this._list[this._idx].setAttribute(fname,val);}
 
leave_iterator.prototype.getGross = function () { return this._list[this._idx].getAttribute("gross") || "0";} 
leave_iterator.prototype.setGross = function(g) { this._list[this._idx].setAttribute("gross",g);}

leave_iterator.prototype.getSick = function () { return this._list[this._idx].getAttribute("sick") || "0";} 
leave_iterator.prototype.setSick = function(g) { this._list[this._idx].setAttribute("sick",g);}
 
leave_iterator.prototype.getLeave = function () { return this._list[this._idx].getAttribute("leave") || "0";} 
leave_iterator.prototype.setLeave = function(g) { this._list[this._idx].setAttribute("leave",g);}

leave_iterator.prototype.getHome = function () { return this._list[this._idx].getAttribute("home") || "0";} 
leave_iterator.prototype.setHome = function(g) { this._list[this._idx].setAttribute("home",g);}

leave_iterator.prototype.getStrike = function () { return this._list[this._idx].getAttribute("strike") || "0";} 
leave_iterator.prototype.setStrike = function(g) { this._list[this._idx].setAttribute("strike",g);}

  // classification days
leave_iterator.prototype.getDclas = function () { return this._list[this._idx].getAttribute("dclas") || "0";} 
leave_iterator.prototype.setDclas = function(g) { this._list[this._idx].setAttribute("dclas",g);}


leave_iterator.prototype.getOther = function () { return this._list[this._idx].getAttribute("other") || "0";} 
leave_iterator.prototype.setOther = function(g) { this._list[this._idx].setAttribute("other",g);}

  // Targets for year
leave_iterator.prototype.getTgtSR = function () { return this._list[this._idx].getAttribute("tgtsr") || "0";} 
leave_iterator.prototype.setTgtSR = function(g) { this._list[this._idx].setAttribute("tgtsr",g);}

leave_iterator.prototype.getTgtFA = function () { return this._list[this._idx].getAttribute("tgtfa") || "0";} 
leave_iterator.prototype.setTgtFA = function(g) { this._list[this._idx].setAttribute("tgtfa",g);}

leave_iterator.prototype.getTgtProd = function () { return this._list[this._idx].getAttribute("tgtprod") || "1.8";} 
leave_iterator.prototype.setTgtProd = function(g) { this._list[this._idx].setAttribute("tgtprod",g);}

  

leave_iterator.prototype.getNet   = function () { return parseFloat(this.getGross()) 
				- parseFloat(this.getSick())
				- parseFloat(this.getLeave())
				- parseFloat(this.getHome())
				- parseFloat(this.getStrike())
				- parseFloat(this.getOther())
				- parseFloat(this.getDclas())
				;
  }
leave_iterator.prototype.getValue = function() { return this._list[this._idx];}    




function year_iterator(year) {
  this._idx=0;
  this._end=12;
}

year_iterator.prototype.end = function() {return this._idx>= this._end;}
year_iterator.prototype.next = function() { this._idx++ ;}
  
year_iterator.prototype.getMonthName  = function () { return LMONTHS[this._idx];}
year_iterator.prototype.getMonth   = function() { return this._idx+1; }

